/**
 * 
 */
package com.et114.modules.org.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

//import com.et114.components.acegi.Constants;
//import com.et114.core.exception.CheckException;
//import com.et114.modules.org.vo.FunctionVo;
//import com.et114.components.acegi.Constants;
import com.et114.components.acegi.Constants;
import com.et114.components.acegi.cache.AcegiCacheManager;
import com.et114.components.ectable.EXTableUtils;
import com.et114.core.action.BaseDispatchAction;
import com.et114.core.common.CONST;
import com.et114.core.common.Error;
import com.et114.modules.org.form.FuncAttachForm;
import com.et114.modules.org.message.FuncMessageManager;
import com.et114.modules.org.message.OrgMessageManager;
import com.et114.modules.org.services.FunctionManager;
import com.et114.modules.org.utility.SessionUserInfo;
import com.et114.modules.org.vo.FunctionAttachVo;
import com.et114.modules.org.vo.FunctionVo;
//import com.et114.components.acegi.cache.AcegiCacheManager;

import org.apache.commons.beanutils.PropertyUtils;

import java.util.List;


/*
 * @author guanhw
 */
public class FuncAttachAction extends BaseDispatchAction {
	
	/**
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public  ActionForward loadAttachs( ActionMapping mapping, 
			                            ActionForm form ,
										HttpServletRequest request , 
										HttpServletResponse response ) throws Exception {
		FuncAttachForm faForm = ( FuncAttachForm ) form ;
		FunctionManager fmanager = ( FunctionManager )  getBean( "functionManager" ) ;
		
		Long funcId = faForm.getFuncId ( ) ; 
		//valid 
		/*
		boolean leaf = fmanager.getFunc ( funcId ).isLeaf ( ) ;
		if ( !leaf ) {
			Error error = new Error ( );
			error.setErrorTitle ( OrgMessageManager.get ( ).getMessage ( "org.funcurl.exist" ,request.getLocale ( )   ) ); 
			request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY ,	error );	
			if ( faForm.getId ( ) == null || faForm.getId ( ).longValue ( ) == 0L )
				 return mapping.findForward ( "glerror" );	
			
		}*/
		FunctionVo funcVo = fmanager.getFunc ( funcId ) ; 
		if ( funcVo == null || !funcVo.isLeaf ( ) ) {
			error.setErrorTitle ( FuncMessageManager.get ( ).getMessage ( "org.func.isnull" , request.getLocale ( ) ) );
			request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
			return mapping.findForward( "glerror" ) ;								
		}					
		
		List attachs = fmanager.getFuncAttachsByFuncId ( funcId ) ;
		request.setAttribute (  "attachs" , attachs ) ;
		return mapping.findForward ( "tofuncAttachInfos" ) ;
	}

	public  ActionForward loadAttach( ActionMapping mapping, 
							            ActionForm form ,
										HttpServletRequest request , 
										HttpServletResponse response ) throws Exception {
		FuncAttachForm faForm = ( FuncAttachForm ) form ;
		FunctionManager fmanager = ( FunctionManager )  getBean( "functionManager" ) ;
		
		Long id = faForm.getId ( );
		FunctionAttachVo vo = fmanager.getFuncAttach ( id ) ; 
		if ( vo == null  ) {
			error.setErrorTitle ( FuncMessageManager.get ( ).getMessage ( "org.func.isnull" , request.getLocale ( ) ) );
			request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY , error ) ;
			return mapping.findForward( "glerror" ) ;								
		}					

		request.setAttribute ( "vo" , vo ) ; 
		return mapping.findForward ( "loadAttach" ) ;
	}
	
	
	public  ActionForward newAttach( ActionMapping mapping, 
							            ActionForm form ,
										HttpServletRequest request , 
										HttpServletResponse response ) throws  Exception {
		SessionUserInfo currentUser = getCurrentUser ( request ) ; 
		FuncAttachForm faForm = ( FuncAttachForm ) form ;
		FunctionManager fmanager = ( FunctionManager )  getBean( "functionManager" ) ;
		//vaild
		String url = faForm.getSecureUrl ( ) ;
		String res = null ;
		if ( url != null && !Constants.NULL_URL .equals ( url.trim ( ) ) && !"".equals ( url.trim ( ) )   ){
			res = AcegiCacheManager.getInstansce ( ).cacheAntContains ( url ) ; 
			if ( res != null ) {
				Error error = new Error ( );
				error.setErrorTitle ( OrgMessageManager.get ( ).getMessage ( "org.funcurl.exist" ,request.getLocale ( )   ) ); 
				request.setAttribute ( CONST.VIEW_LAYER_EXCEPTION_KEY ,	error );	
				if ( faForm.getId ( ) == null || faForm.getId ( ).longValue ( ) == 0L )
					 return mapping.findForward ( "glerror" );	
				else {
					if ( !fmanager.getFuncAttach ( faForm.getId ( ) ).getSecureUrl ( ).equals ( url ) ) 
						return mapping.findForward ( "glerror" );
				}
			}			
		}
		//
		
		
		FunctionAttachVo fav = new FunctionAttachVo(  ); 
		PropertyUtils.copyProperties ( fav , faForm ) ;
		//valid
		fav.setCrtUser ( currentUser.getUserId ( ) ) ; 

		fmanager.addFuncAttach( fav ) ; 
		if ( faForm.getId ( ) == null  ||  faForm.getId ( ).longValue ( ) == 0  ) {
			StringBuffer to  = new StringBuffer( "funcSuccess.go?type=funcAtt" ) ; 
			if ( faForm.getFuncId ( ) != null && faForm.getFuncId ( ).longValue ( ) != 0 ) {
				to.append ( "&funcId=" ) .append ( faForm.getFuncId ( ) ) ;
			}
			return new ActionForward( to.toString ( ) , true ) ;
		}
		
		return mapping.findForward ( "glsuccess" ) ;
	}
	
	public  ActionForward deleteAttach( ActionMapping mapping, 
							            ActionForm form ,
										HttpServletRequest request , 
										HttpServletResponse response ) throws Exception {
		StringBuffer to  = new StringBuffer( "funcAttach.go?method=loadAttachs&funcId=" );
		
		FuncAttachForm faForm = ( FuncAttachForm ) form ;
		FunctionManager fmanager = ( FunctionManager )  getBean( "functionManager" ) ;
		
		Long funcId = faForm.getFuncId ( ) ; 
		Long[] deleteIds = faForm.getDeleteId ( ) ; 
		
		to.append ( funcId ) ;
		String params = EXTableUtils.getLimitParams ( request ) ;
		to.append ( params ) ;
		
		
		if ( deleteIds != null ) {
			for ( int i = 0 ; i < deleteIds.length ; i ++ ) {
				Long deleteId = deleteIds[ i ] ;
				fmanager.deleteFuncAttachById ( funcId , deleteId , getCurrentUser ( request ).getUserId ( ) ) ;
			}
		}
		return new ActionForward( to.toString ( ) , true ) ;
	}
	
}
